package com.zja.www.dao.impl;

import com.zja.www.dao.MedicalRecordDao;
import com.zja.www.entity.MedicalRecord;
import com.zja.www.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MedicalRecordDaoImpl implements MedicalRecordDao {
    private final Connection connection;

    public MedicalRecordDaoImpl() {
        this.connection = DBUtil.getConnection();
        if (this.connection == null) {
            throw new RuntimeException("无法获取数据库连接");
        }

    }

    @Override
    public boolean insert(MedicalRecord record) {
        String sql = "INSERT INTO medical_record (user_id, doctor_id, record_time, status, type) VALUES (?, ?, ?, ?, ?)";
        try (PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            ps.setInt(1, record.getUserId());
            ps.setInt(2, record.getDoctorId());
            ps.setTimestamp(3, record.getRecordTime());
            ps.setString(4, record.getStatus());
            ps.setString(5, record.getType());

            int affectedRows = ps.executeUpdate();
            if (affectedRows > 0) {
                try (ResultSet rs = ps.getGeneratedKeys()) {
                    if (rs.next()) {
                        record.setId(rs.getInt(1));
                    }
                }
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public MedicalRecord selectById(int id) {
        String sql = "SELECT * FROM medical_record WHERE id = ?";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, id);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    return mapResultSetToRecord(rs);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<MedicalRecord> selectByDoctorAndTime(int doctorId, Timestamp date) {
        List<MedicalRecord> records = new ArrayList<>();
        String sql = "SELECT * FROM medical_record WHERE doctor_id = ? AND DATE(record_time) = DATE(?)";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, doctorId);
            ps.setTimestamp(2, date);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    records.add(mapResultSetToRecord(rs));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return records;
    }

    @Override
    public boolean updateStatus(int recordId, String newStatus) {
        String sql = "UPDATE medical_record SET status = ? WHERE id = ?";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, newStatus);
            ps.setInt(2, recordId);
            return ps.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<MedicalRecord> selectByUserId(int userId) {
        List<MedicalRecord> records = new ArrayList<>();
        String sql = "SELECT * FROM medical_record WHERE user_id = ?";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, userId);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    records.add(mapResultSetToRecord(rs));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return records;
    }

    @Override
    public boolean delete(int id) {
        String sql = "DELETE FROM medical_record WHERE id = ?";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, id);
            return ps.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    private MedicalRecord mapResultSetToRecord(ResultSet rs) throws SQLException {
        MedicalRecord record = new MedicalRecord();
        record.setId(rs.getInt("id"));
        record.setUserId(rs.getInt("user_id"));
        record.setDoctorId(rs.getInt("doctor_id"));
        record.setRecordTime(rs.getTimestamp("record_time"));
        record.setStatus(rs.getString("status"));
        record.setType(rs.getString("type"));
        return record;
    }

}
